package fr.bda.cloud.tray.file;

import java.io.File;

import javax.swing.JOptionPane;

import fr.bda.cloud.tray.CloudTray;
import fr.bda.cloud.tray.CloudTrayDialog;
import fr.bda.cloud.tray.CloudTrayDialogController;
import fr.bda.cloud.tray.CloudTrayDialogDataValidator;

public class SaveFileDialogValidator extends CloudTrayDialogDataValidator {

	public SaveFileDialogValidator(CloudTray cloudTray) {
		super(cloudTray);
	}

	@Override
	public <C extends CloudTrayDialogController> boolean validate(CloudTrayDialog<C> dialog) {
		StringBuilder errorBuffer = new StringBuilder();

		if (SaveFileDialog.class.isInstance(dialog)) {

			SaveFileDialog saveFileDialog = (SaveFileDialog) dialog;
			String path = saveFileDialog.pathField.getText().trim();
			if (path.length() == 0) {
				errorBuffer.append("- You need to select a file on your file system\n");

			} else {
				File f = new File(path);
				if (!f.exists() || !f.isFile()) {
					errorBuffer.append("- You need to select an existing file on your file system\n");
				}
			}

			if (saveFileDialog.selectedStorageUnit.getModel().getSize() == 0) {
				errorBuffer.append("- You must select at least one storage unit\n");
			}

		}

		if (errorBuffer.length() > 0) {
			JOptionPane.showMessageDialog(dialog, errorBuffer.toString(), "Validation fail", JOptionPane.WARNING_MESSAGE);
		}

		return errorBuffer.length() == 0;
	}
}
